"""
语法
    ActionChains(driver).鼠标事件方法.perform()
        - move_to_element(元素)  # 鼠标悬停
        - drag_and_drop(起始元素,目标元素)  # 鼠标拖拽
        - drag_and_drop_by_offset(起始元素,x,y)
    - 注意：所有的鼠标事件，都需要perform()执行
"""

# 1. 导包
import os
from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

# 2. 创建浏览器驱动,并打开浏览器
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("http://www.baidu.com")
driver.get(os.path.abspath("../练习页面/drop.html"))

# 3. 元素操作+元素定位
# 3.1 鼠标悬停
# 定位需要悬停的元素--设置
# 3. 元素定位+元素操作
# 3.1 鼠标悬停
# 定位需要悬停的元素--设置
# setting = driver.find_element(By.ID, "s-usersetting-top")
# 执行鼠标悬停事件
# ActionChains(driver).move_to_element(setting).perform()

# 3.2 鼠标拖拽
red = driver.find_element(By.ID, "div1")
green = driver.find_element(By.ID, "div2")

# ActionChains(driver).drag_and_drop(red, green).perform()

ActionChains(driver).drag_and_drop_by_offset(red, 100, 100).perform()

# 4. 关闭浏览器
sleep(3)
driver.quit()
